<template>
  <div class="sub-category">
    <div class="contain er">
        <!-- 组件功能 -->
        <!-- 面包屑 -->
<SubBread/>
<!-- 筛选条件 -->
<SubFilter/>
  <div class="goods-list">
        <!-- 排序组件 -->
        <SubSort />
        <!-- 商品列表 -->
        <SubList/>
      </div>
    </div>
  </div>
</template>

<script>
import { findSubCategoryFilter } from '@/api/category'
import { ref, onMounted, provide } from 'vue'
import { useRoute } from 'vue-router'
import SubBread from './components/sub-bread.vue'
import SubFilter from './components/sub-filter.vue'
import SubSort from './components/sub-sort.vue'
import SubList from './components/sub-list.vue'
export default {
  name: 'SubCategory',
  components: {
    SubBread,
    SubFilter,
    SubSort,
    SubList
  },
  setup () {
    const subCateData = ref({})
    const route = useRoute()
    // 注入数据=>顶层注入一次，下边的子孙组件都可以使用这个数据
    provide('subCateData', subCateData)
    // 提供过滤条件数据(列表后台接口需要的条件)
    const filterParams = ref({
      sortField: null, // 排序类别
      attrs: [], // 商品属性
      brandId: null // 品牌名称
    })
    provide('filterParams', filterParams)

    const getSubCateData = async () => {
      const { result } = await findSubCategoryFilter(route.params.id)
      console.log('二级分类数据：', result)
      // 添加筛选条件默认全部条件数据和选中状态数据
      // 1. 品牌
      // 后台品牌都添加一个是否选中的属性=》false
      result.brands.forEach(brand => {
        brand.selected = false
      })
      // 自己添加全部条件，默认选中
      result.brands.unshift({
        id: null,
        name: '全部',
        selected: true
      })
      // 2. 属性
      result.saleProperties.forEach(item => {
        item.properties.forEach(per => {
          per.selected = false
        })
        item.properties.unshift({
          id: null,
          name: '全部',
          selected: true
        })
      })
      subCateData.value = result
    }
    onMounted(() => {
      getSubCateData()
    })
  }
}
</script>
<style scoped lang='less'>
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
}
</style>
